<?php
namespace app\admin\controller;
use app\admin\controller\BaseAdmin;
use app\admin\model\Shareimg;
use function Couchbase\defaultDecoder;
use Data;
use GatewayWorker\Lib\Gateway;
use think\Db;
use think\Request;
use app\admin\model\Company as CompanyModel;

class Common extends BaseAdmin
{

    /**
     * 列表
     * @return mixed
     */
    public function index(Request $request){
        if (request()->isAjax()){
            return CompanyModel::getlist($request);
        }else{

            //获取权限菜单
            $menu = Db::name('menu')->order('sort','asc')->select();
            //将菜单id作为数组key
            $keys = array_column($menu,'id');
            $menu = array_combine($keys,$menu);
            $menus = Data::channelLevel($menu);
            $this->assign('menus_lists',$menus);
            $pageno = input('param.pageno', 1);
            return $this->fetch();
            $this->assign('pageno', $pageno);
        }
    }

    public function getRegText()
    {
        $text = \app\index\model\Common::getRegText();
        $this->ajaxReturn(['regtxt'=>$text],'success',200);
    }

    public function storeRegText(Request $request)
    {
        $content = $request->get('content','');
        $regTextObj = \app\index\model\Common::where('param', 'regtext')
            ->find();
        if ($regTextObj) {
            $regTextObj->content = $content;

        } else {
            $regTextObj = new \app\index\model\Common([
                'content' => $content,
                'param' => 'regtext',
            ]);
        }
        $regTextObj->save();

         return $this->ajaxReturn('','成功',200) ;
    }

    #背景图
    public function getBgimg()
    {
        $data = Shareimg::order('sort','asc')->field(['bgimg'])->select();
        return returnData($data);
    }

    public function showBgimghtml(){
        return $this->fetch();
    }
    public function setBgimg(){
        $data = request()->post('ad');
        (new Shareimg())->whereOrRaw('1=1')->delete();
        if (is_array($data)){
            foreach ($data as $item) {
                Shareimg::create($item);
            }
        }

        return returnData();
    }


    public function gethelp()
    {
        if (request()->isAjax()) {
            $text = \app\index\model\Common::getHelp();
            $this->ajaxReturn(['help'=>$text],'success',200);
        } else {
            $text = \app\index\model\Common::getHelp();
//            $text = html_entity_decode($text);
            $this->assign('text', $text);
            return $this->fetch('help');
        }
    }

    public function storeHelp(Request $request)
    {
        $content = $request->post('text','');

        $regTextObj = \app\index\model\Common::where('param', 'help')
            ->find();
        if ($regTextObj) {
            $regTextObj->content = $content;
        } else {
            $regTextObj = new \app\index\model\Common([
                'content' => $content,
                'param' => 'help',
            ]);
        }
        $regTextObj->save();
        return $this->ajaxReturn($regTextObj,'成功',200) ;
    }
}
